package com.work.controller;

import com.google.gson.Gson;
import com.work.bean.Notice;
import com.work.bean.Page;
import com.work.dao.NoticeDao;
import com.work.dao.impl.NoticeDaoImpl;
import com.work.service.NoticeService;
import com.work.service.impl.NoticeServiceImpl;
import com.work.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

public class NoticeServlet extends BaseServlet {
    NoticeDao noticeDao = new NoticeDaoImpl();
    NoticeService noticeService = new NoticeServiceImpl();
    protected void queryNotice(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        List<Notice> notices = noticeService.queryNotice();
        req.setAttribute("notices",notices);
        req.getRequestDispatcher("/pages/board/board_look.jsp").forward(req,resp);
    }
    protected void queryNotice2(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        List<Notice> notices = noticeService.queryNotice();
        System.out.println(notices);
        Gson gson=new Gson();
        String json=gson.toJson(notices);
        System.out.println(json);
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out=resp.getWriter();
        out.println(json);
        req.setAttribute("pageNo",1);
    }
    protected void queryNoticeById(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        Notice notice = noticeService.queryNoticeById(id);
        req.setAttribute("notice",notice);
        req.getRequestDispatcher("/pages/board/board_look.jsp").forward(req,resp);
    }
    protected void queryNoticeById1(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        Notice notice = noticeService.queryNoticeById(id);
        req.setAttribute("notice1",notice);
        req.getRequestDispatcher("/pages/admin/board/admin_board_edit.jsp").forward(req,resp);
    }
    protected void saveNotice(HttpServletRequest req, HttpServletResponse resp)throws
            ServletException, IOException {
        resp.setContentType("text/html; charset=UTF-8");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        Notice notice = new Notice();
        notice.setTitle(title);
        notice.setContent(content);
        notice.setEnd_time(new Date());
        noticeService.saveNotice(notice);
        req.getRequestDispatcher("/pages/admin/board/admin_board.jsp").forward(req,resp);
    }
    protected void deleteNotice(HttpServletRequest req, HttpServletResponse resp)throws
            ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        noticeService.deleteNotice(id);
        req.getRequestDispatcher("/pages/admin/board/admin_board.jsp").forward(req,resp);
    }
    protected void updateNotice(HttpServletRequest req, HttpServletResponse resp)throws
            ServletException, IOException {
        Notice notice = WebUtils.copyParamToBean(req.getParameterMap(),new Notice());
        noticeService.updateNotice(notice);
        req.getRequestDispatcher("/pages/admin/board/admin_board.jsp").forward(req,resp);
    }
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = 10;
        Page<Notice> page = noticeService.page(pageNo,pageSize);
        page.setUrl("NoticeServlet?action=page");
        req.setAttribute("page",page);
        req.getRequestDispatcher("/pages/student/student.jsp").forward(req,resp);
    }
}
